//
//  YYAudioTool.m
//  音乐播放器
//
//  Created by zpy on 15/11/28.
//  Copyright © 2015年 zpy. All rights reserved.
//

#import "YYAudioTool.h"

@implementation YYAudioTool

static NSMutableDictionary *_musics;
+(void)initialize {

    _musics = [NSMutableDictionary dictionary];
}
// 播放音乐
+(AVAudioPlayer *)playMusicWithName: (NSString *) musicName{

    assert(musicName);
    
    AVAudioPlayer *player = _musics[musicName];
    if (player == nil) {
        // 获取资源的URL
        NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
        [player prepareToPlay];
        // 存入字典中
        _musics[musicName] = player;
    }
    [player play];
    return player;
}
// 暂停音乐
+(void)pauseMusicWithName:(NSString *)musicName{

    assert(musicName);
    AVAudioPlayer *player = _musics[musicName];
    if (player) {
        [player pause];
    }
}
// 停止音乐
+(void)stopMusicWithName:(NSString *)musicName {

    assert(musicName);
    AVAudioPlayer *player = _musics[musicName];
    if (player) {
        [player stop];
        player = nil;
        [_musics removeObjectForKey:musicName];
    }
}
@end
